package com.library.entity.po;

import java.util.Objects;

/**
 * 图书借阅排行
 */
public class BookRankPO implements Comparable<BookRankPO> {

    private String bname; // 图书名称
    private Integer count; // 借阅次数

    public BookRankPO() {
    }

    public BookRankPO(String bname, Integer count) {
        this.bname = bname;
        this.count = count;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }


    @Override
    public String toString() {
        return "BookRankPO{" +
                "bname='" + bname + '\'' +
                ", count=" + count +
                '}';
    }

    // 重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BookRankPO bookRankPO = (BookRankPO) o;
        return Objects.equals(bname, bookRankPO.bname);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bname);
    }

    // 实现比较方法：先按借阅次数降序，再按书名升序
    @Override
    public int compareTo(BookRankPO other) {
        // 处理count为null的情况
        int thisCount = this.count != null ? this.count : 0;
        int otherCount = other.count != null ? other.count : 0;
        
        int countCompare = Integer.compare(otherCount, thisCount);
        if (countCompare != 0) {
            return countCompare;
        }
        
        // 处理bname为null的情况
        String thisBname = this.bname != null ? this.bname : "";
        String otherBname = other.bname != null ? other.bname : "";
        
        return thisBname.compareTo(otherBname);
    }

}
